// leaderPerson/pages/confirm_detail/confirm_detail.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        date: "",
        type: 1,
        time: 1,
        extract: 0,
        chooseNum: 0,
        chooseAll: 1,
        hasPro: {},
        noPro: {}
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            clerk: wx.getStorageSync("clerk"),
            date: options.date || ""
        });
        this.getBase();
        this.getLeaderWait();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.leader_title ? res.extra.leader_title + "收货" : "团长收货"
                });
                that.setData({
                    color_main: res.extra.color_main,
                    time_choose: res.extra.time_choose,
                    apptitle: res.extra.title,
                    leader_share: res.extra.leader_share,
                    user_share: res.extra.user_share,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    if_leadersales_show: res.extra.if_leadersales_show,
                    leader_detail_edit: res.extra.leader_detail_edit,
                    phone: res.extra.phone
                });
            }
        });
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    chooseType: function(e) {
        this.setData({
            type: e.currentTarget.dataset.type
        });
        this.getLeaderWait();
    },
    getValue: function(e) {
        this.setData({
            keyword: e.detail.value
        });
        this.getLeaderWait();
    },
    getLeaderWait: function() {
        let _this = this;
        let clerk = _this.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0 || clerk.wom_status == undefined) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var params = {
            date: _this.data.date,
            openid: openid,
            type: _this.data.type,
            keyword: _this.data.keyword,
            delivery_channel: _this.data.extract
        };
        common.request({
            endpoint: "My/waitpick/leader_wait_detail3",
            method: "post",
            data: params,
            success: res => {
                if (_this.data.type == 1) {
                    var chooseNum = 0;
                    res.extra.hasPro.proInfo.filter(x => {
                        x.choose = 1;
                        if (x.choose == 1) {
                            chooseNum++;
                        }
                    });
                    _this.setData({
                        hasPro: res.extra.hasPro,
                        noPro: res.extra.noPro,
                        chooseNum: chooseNum
                    });
                } else {
                    var chooseNum = 0;
                    var has = res.extra.has;
                    has.filter(x => {
                        x.pro_info.map(item => {
                            item.choose = 1;
                            if (item.choose == 1) {
                                chooseNum++;
                            }
                        });
                    });
                    _this.setData({
                        has: res.extra.has,
                        no: res.extra.no,
                        chooseNum: chooseNum
                    });
                }
            }
        });
    },
    choice1: function(e) {
        let id = e.currentTarget.dataset.id;
        var hasPro = this.data.hasPro;
        var chooseNum = 0;
        hasPro.proInfo.map(x => {
            if (id == x.id) {
                x.choose = x.choose == 0 ? 1 : 0;
            }
            if (x.choose == 1) {
                chooseNum++;
            }
        });
        this.setData({
            hasPro: hasPro,
            chooseNum: chooseNum
        });
    },
    choice2: function(e) {
        let id = e.currentTarget.dataset.id;
        var has = this.data.has;
        var chooseNum = 0;
        has.map(item => {
            item.pro_info.map(x => {
                if (id == x.id) {
                    x.choose = x.choose == 0 ? 1 : 0;
                }
                if (x.choose == 1) {
                    chooseNum++;
                }
            });
        });
        this.setData({
            has: has,
            chooseNum: chooseNum
        });
    },
    chooseExtract: function(e) {
        this.setData({
            extract: e.currentTarget.dataset.extract
        });
        this.getLeaderWait();
    },
    //一键收货
    totalButt: common.throttle(function(e) {
        var that = this;
        var item_ids = [];
        if (that.data.type == 1) {
            var proInfo = that.data.hasPro.proInfo;
            proInfo.map(x => {
                if (x.choose == 1) {
                    item_ids.push(x.id);
                }
            });
        } else {
            var proInfo = that.data.has;
            proInfo.map(item => {
                item.pro_info.map(x => {
                    if (x.choose == 1) {
                        item_ids.push(x.id);
                    }
                });
            });
        }
        let num = 0;
        common.request({
            endpoint: "My/waitpick/leader_receive2",
            method: "post",
            data: {
                item_ids: item_ids.join(","),
                //item_ids
                openid: wx.getStorageSync("user").openid
            },
            success: res => {
                wx.showToast({
                    duration: 1500,
                    icon: "none",
                    title: "一键收货成功" || res.message
                });
                that.getLeaderWait();
                wx.navigateTo({
                    url: "/leaderPerson/pages/receivedSuc/receivedSuc?id=" + item_ids.join(",")
                });
            },
            fail(res) {
                if (res.code == 2) {
                    wx.showToast({
                        icon: "none",
                        title: res.message
                    });
                } else {
                    wx.showToast({
                        icon: "none",
                        title: "取货失败"
                    });
                }
            }
        });
    }, 2e3)
});